package com.ktgj.customer.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.customer.domain.Employee;
import com.ktgj.customer.mapper.EmployeeMapper;
import com.ktgj.customer.service.ILoginService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.CrmTitleMapper;
import com.ktgj.customer.domain.CrmTitle;
import com.ktgj.customer.service.ICrmTitleService;

import javax.servlet.http.HttpServletRequest;

/**
 * CRM跑马灯维护Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-05
 */
@Service
public class CrmTitleServiceImpl implements ICrmTitleService 
{
    @Autowired
    private CrmTitleMapper crmTitleMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询CRM跑马灯维护
     * 
     * @param id CRM跑马灯维护ID
     * @return CRM跑马灯维护
     */
    @Override
    public CrmTitle selectCrmTitleById(Long id)
    {
        return crmTitleMapper.selectCrmTitleById(id);
    }

    /**
     * 查询CRM跑马灯维护列表
     * 
     * @param crmTitle CRM跑马灯维护
     * @return CRM跑马灯维护
     */
    @Override
    public List<CrmTitle> selectCrmTitleList(CrmTitle crmTitle)
    {
        return crmTitleMapper.selectCrmTitleList(crmTitle);
    }

    /**
     * 新增CRM跑马灯维护
     * 
     * @param crmTitle CRM跑马灯维护
     * @return 结果
     */
    @Override
    public int insertCrmTitle(CrmTitle crmTitle)
    {
        return crmTitleMapper.insertCrmTitle(crmTitle);
    }

    /**
     * 修改CRM跑马灯维护
     * 
     * @param crmTitle CRM跑马灯维护
     * @return 结果
     */
    @Override
    public int updateCrmTitle(CrmTitle crmTitle)
    {
        return crmTitleMapper.updateCrmTitle(crmTitle);
    }

    /**
     * 批量删除CRM跑马灯维护
     * 
     * @param ids 需要删除的CRM跑马灯维护ID
     * @return 结果
     */
    @Override
    public int deleteCrmTitleByIds(Long[] ids)
    {
        return crmTitleMapper.deleteCrmTitleByIds(ids);
    }

    /**
     * 删除CRM跑马灯维护信息
     * 
     * @param id CRM跑马灯维护ID
     * @return 结果
     */
    @Override
    public int deleteCrmTitleById(Long id)
    {
        return crmTitleMapper.deleteCrmTitleById(id);
    }

    /**
     * 跑马灯列表
     * */
    @Override
    public List<CrmTitle> marquee() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<CrmTitle> marquee = crmTitleMapper.marquee();
        if (marquee != null && marquee.size() > 0){
            for (CrmTitle crmTitle : marquee) {
                if (crmTitle.getEmployeeid() != null){
                    Employee employee = employeeMapper.backSelectEmployee(crmTitle.getEmployeeid());
                    if (employee != null){
                        crmTitle.setEmployeeName(employee.getEmployeeName());
                    }
                }

                if (crmTitle.getCreateTime() != null){
                    crmTitle.setCreateDate(format.format(crmTitle.getCreateTime()));
                }
            }
        }
        return marquee;
    }

    /**
     * 添加跑马灯
     * */
    @Override
    public int addMarquee(HttpServletRequest request, String name) throws Exception {
        Employee loginEmployee = loginService.getLoginEmployee(request);
        if (loginEmployee == null){
            throw new Exception("登录已过期");
        }
        CrmTitle crmTitle = new CrmTitle();
        crmTitle.setName(name);
        crmTitle.setEmployeeid(loginEmployee.getEmployeeId());
        crmTitle.setCreateTime(new Date());
        crmTitle.setIsshow("true");
        crmTitleMapper.start("false");
        int i = crmTitleMapper.insertCrmTitle(crmTitle);
        return i;
    }

    /**
     * 跑马灯显示
     * */
    @Override
    public String show() {
        CrmTitle crmTitle = crmTitleMapper.show();
        if (crmTitle != null){
            return crmTitle.getName();
        }
        else {
            return "";
        }
    }

    /**
     * 启用跑马灯
     * */
    @Override
    public int enable(Long id) {
        int aFalse = crmTitleMapper.start("false");
        if (aFalse > 0){
            CrmTitle crmTitle = new CrmTitle();
            crmTitle.setId(id);
            crmTitle.setIsshow("true");
            int i = crmTitleMapper.updateCrmTitle(crmTitle);
            return i;
        }

        return 0;
    }
}
